﻿
Namespace LightSwitchApplication

    Public Class ApplicationDataService

        Private Sub Locations_Inserting(entity As Location)
            Dim change = entity.LocationsChangeLogs.AddNew
            change.ChangeType = "Inserted"
            change.Updated = Now()
            change.ChangedBy = Me.Application.User.FullName

            Dim newvals = "Inserted Values:"
            For Each prop In entity.Details.Properties.All().
                OfType(Of Microsoft.LightSwitch.Details.IEntityStorageProperty)()

                If prop.Name <> "Id" Then
                    newvals += String.Format("{0}{1}: {2}", vbCrLf, prop.Name, prop.Value)
                End If
            Next
            For Each prop In entity.Details.Properties.All().
                OfType(Of Microsoft.LightSwitch.Details.IEntityReferenceProperty)()

                If prop.Name <> "Id" Then
                    newvals += String.Format("{0}{1}: {2}", vbCrLf, prop.Name, prop.Value)
                End If
            Next
            change.NewValues = newvals
        End Sub

        Private Sub Locations_Updating(entity As Location)
            Dim change = entity.LocationsChangeLogs.AddNew
            change.ChangeType = "Updated"
            change.Updated = Now()
            change.ChangedBy = Me.Application.User.FullName

            Dim newvals = "New Values:"
            Dim oldvals = "Original Values:"
            For Each prop In entity.Details.Properties.All().
                 OfType(Of Microsoft.LightSwitch.Details.IEntityStorageProperty)()

                If prop.Name <> "Id" Then
                    If Not Object.Equals(prop.Value, prop.OriginalValue) Then
                        oldvals += String.Format("{0}{1}: {2}", ControlChars.Lf, prop.Name, prop.OriginalValue)
                        newvals += String.Format("{0}{1}: {2}", ControlChars.Lf, prop.Name, prop.Value)
                    End If
                End If
            Next

            For Each prop In entity.Details.Properties.All().
                 OfType(Of Microsoft.LightSwitch.Details.IEntityReferenceProperty)()

                If prop.Name <> "Id" Then
                    If Not Object.Equals(prop.Value, prop.OriginalValue) Then
                        oldvals += String.Format("{0}{1}: {2}", ControlChars.Lf, prop.Name, prop.OriginalValue)
                        newvals += String.Format("{0}{1}: {2}", ControlChars.Lf, prop.Name, prop.Value)
                    End If
                End If
            Next


            change.OriginalValues = oldvals
            change.NewValues = newvals
        End Sub

        Private Sub Locations_Deleting(entity As Location)
            Dim change = entity.LocationsChangeLogs.AddNew
            change.ChangeType = "Deleted"
            change.Updated = Now()
            change.ChangedBy = Me.Application.User.FullName

            Dim oldvals = "Deleted Values:"
            For Each prop In entity.Details.Properties.All().
                OfType(Of Microsoft.LightSwitch.Details.IEntityStorageProperty)()

                If prop.Name <> "Id" Then
                    oldvals += String.Format("{0}{1}: {2}", vbCrLf, prop.Name, prop.Value)
                End If
            Next

            For Each prop In entity.Details.Properties.All().
                OfType(Of Microsoft.LightSwitch.Details.IEntityReferenceProperty)()

                If prop.Name <> "Id" Then
                    oldvals += String.Format("{0}{1}: {2}", vbCrLf, prop.Name, prop.Value)
                End If
            Next
            change.OriginalValues = oldvals
        End Sub

    End Class

End Namespace
